'use strict';
require('./index.css');
require('page/common/nav/index.js');
require('page/common/header/index.js');
var navSide         = require('page/common/nav-side/index.js');
var _mm             = require('util/mm.js');
var _message        = require('service/message-service.js');
var _user           = require('service/user-service.js');
var templateIndex   = require('./index.string');

// page 逻辑部分
var page = {
    init: function(){
        this.onLoad();
        this.bindEvent();
    },
    onLoad : function(){
        // 初始化左侧菜单
        navSide.init({
            name: 'become-tutor'
        });
        // 加载用户信息
        this.loadUserInfo();
    },
    bindEvent : function(){
        var _this = this;
        // 点击提交按钮后的动作
        $(document).on('click', '.btn-submit', function(){
            var tutorData = {
                    tutorCard        : $.trim($('#idcard').val()),
				    tutorGender	     : $.trim($('input:radio:checked').val()),
					tutorEducation   : $.trim($('#stateId').val()),
					applycause       : $.trim($('#content').val())
                };
	         alert(tutorData);
               // 申请成为教员
            _message.applyTutor(tutorData, function(res, msg){				   
					_mm.successTips(msg);
					  window.location.href = './become-tutor.html';
                }, function(errMsg){
                    _mm.errorTips(errMsg);
					window.location.href = './become-tutor.html';
                });          
        });
    },
    // 加载用户信息
    loadUserInfo : function(){
		var _this    = this;
        var userHtml = '';
        _user.getUserInfo(function(res){
			//alert(res.user.role);
				_this.dataFilter(res);
				userHtml = _mm.renderHtml(templateIndex, res);
				$('.panel-body').html(userHtml);
			
        }, function(errMsg){
            _mm.errorTips(errMsg);
        });
    },
	// 数据的适配
	dataFilter : function(data){
		data.init           = data.user.role        == 0;
	    data.apply          = data.tutorInfo.status == 10;
	    data.applySuccess   = data.tutorInfo.status == 20;
		data.reApply          = data.tutorInfo.status == 30;
		data.noApply        = data.tutorInfo.status == 50;
		data.refuse         = data.tutorInfo.status == 0;
	}
};
$(function(){
    page.init();
});